//
//  HPReleaseTextScrollView.m
//  fruitsDealers
//
//  Created by sgone on 16/4/25.
//  Copyright © 2016年 com.sgone. All rights reserved.
//

#import "HPReleaseTextScrollView.h"
#import "Common.h"
#import "UIView+Helper.h"
#import "Toast+UIView.h"
@interface HPReleaseTextScrollView () <UITextViewDelegate> {
    
    
}

@property (nonatomic, strong)UILabel *placeholderLabel;

//显示字数的lable
@property (nonatomic, weak) UILabel *numLable;

@end

@implementation HPReleaseTextScrollView

+ (instancetype)initWithFrame:(CGRect)frame {
    
    return [[self alloc] initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        _contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _contentTextView.textColor = FDFont_COLOR;
        _contentTextView.font = [UIFont systemFontOfSize:14 / FDLaOt];
        _contentTextView.hidden = NO;
        _contentTextView.delegate = self;
        _contentTextView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_contentTextView];
        
        //        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:) name:@"UITextViewTextDidChangeNotification" object:nil];
        
        _placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, ViewWidth, 20 / FDLaOt)];
        _placeholderLabel.text = @"说点什么吧~ ~";
        _placeholderLabel.enabled = NO;
        _placeholderLabel.textColor = FDGFont_COLOR;
        _placeholderLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
        _placeholderLabel.backgroundColor = [UIColor clearColor];
        [_contentTextView addSubview:_placeholderLabel];
        
        UILabel *numLable = [[UILabel alloc] initWithFrame:CGRectMake(ViewWidth - 100, self.contentTextView.height - 25, 100, 20)];
        numLable.font = [UIFont systemFontOfSize:12 / FDLaOt];
        numLable.textAlignment = NSTextAlignmentRight;
        [self addSubview:numLable];
        self.numLable = numLable;
        
    }
    return self;
}

- (void)textViewDidChange:(UITextView *)textView {
    
    if (textView.text.length == 0) {
        
        _placeholderLabel.text = @"说点什么吧~ ~";
    } else {
        
        _placeholderLabel.text = @"";
    }
    
    NSInteger number = [textView.text length];
    if (number > self.maxLength) {
//        [MBProgressHUD showError:@"最多输入700个字"];
        
        [self makeToast:@"最多输入700个字"];
        textView.text = [textView.text substringToIndex:self.maxLength];
        number = self.maxLength;
        
    }
    self.numLable.text = [NSString stringWithFormat:@"%ld/700",(long)number];
}

@end
